<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: functions.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

defined('_VALID_XTC') or die('Direct Access to this location is not allowed.');

function createImage($srcFile, $destFile, $height = 75, $quality = 95){
	$img = '';

	if (file_exists($srcFile) && isset($destFile)) {
		$size        = getimagesize($srcFile);
		$h           = number_format($height, 0, ',', '');
		$w           = number_format(($size[0] / $size[1]) * $height, 0, ',', '');
		$img =  copyImage($srcFile, $destFile, $w, $h, $quality);
	}
}

function copyImage($srcFile, $destFile, $w, $h, $quality){
    $tmpSrc     = pathinfo(strtolower($srcFile));
    $tmpDest    = pathinfo(strtolower($destFile));
    $size       = getimagesize($srcFile);

    if($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg") {
		$destFile  = substr_replace($destFile, 'jpg', -3);
		$dest      = imagecreatetruecolor($w, $h);
    } elseif ($tmpDest['extension'] == "png")
		$dest = imagecreatetruecolor($w, $h);
    else
		return false;

    switch($size[2]) {
       case 1:       // gif
           $src = imagecreatefromgif($srcFile);
           break;
       case 2:       // jpg
           $src = imagecreatefromjpeg($srcFile);
           break;
       case 3:       // png
           $src = imagecreatefrompng($srcFile);
           break;
       default:
           return false;
           break;
    }

    imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

    switch($size[2]) {
       case 1:
       case 2:
           imagejpeg($dest, $destFile, $quality);
           break;
       case 3:
           imagepng($dest, $destFile);
    }
}

function saveData($action = 'insert') {
	global $db, $message_stack;
	
	if($action == 'insert') {
		if(!is_writeable(DIR_FS_CATALOG_IMAGES.'slider/slider_org/')) {
			$message_stack->add_session('Der Slider-Ordner unter /images/slider/slider_org/ ist nicht beschreibbar!', 'error');
			return;
		}	
	
		$languages = get_languages();
		foreach($languages AS $lang) {
			$image_name = '';
			$slider_array = '';
	
			if($slider_image = &try_upload('slider_pic_'.$lang['id'], DIR_FS_CATALOG_IMAGES.'slider/slider_org/')) {
				$name = explode('.', $slider_image->filename);
				
				$image_name = get_url_friendly_text($name[0]).'.'.$name[1];
				
				createImage(DIR_FS_CATALOG_IMAGES.'slider/slider_org/'.$slider_image->filename, DIR_FS_CATALOG_IMAGES.'slider/slider_big/'.$image_name, 560);
				createImage(DIR_FS_CATALOG_IMAGES.'slider/slider_org/'.$slider_image->filename, DIR_FS_CATALOG_IMAGES.'slider/slider_thumb/'.$image_name);
			
				$slider_array = array(	'image_name' => $image_name,
										'slider_id' => (int)$_POST['slider_id'],
										'title' => $_POST['slider_title_'.$lang['id']],
										'description' => nl2br($_POST['slider_description_'.$lang['id']]),
										'link' => $_POST['slider_link_'.$lang['id']],
										'date_added' => 'NOW()',
										'language_id' => (int)$lang['id']);
				
				$db->db_perform(TABLE_SLIDER_IMAGES, $slider_array);
			}
		}

	} else {
		$languages = get_languages();
		foreach($languages AS $lang) {
			$lng_id = $db->db_query("SELECT 
										image_id
									FROM 
										".TABLE_SLIDER_IMAGES."
									WHERE 
										slider_id = ".$db->db_prepare($_POST['slider_id'])."
									AND 
										language_id = '".(int)$lang['id']."' ");
										
			while(!$lng_id->EOF) {
				$image_name = '';
				$slider_array = '';
				$img_id = $lng_id->fields['image_id'];
				$slider_array = array(	'description' => nl2br($_POST['slider_description_'.$lang['id'].'_'.$img_id]),
										'title' => $_POST['slider_title_'.$lang['id'].'_'.$img_id],
										'link' => $_POST['slider_link_'.$lang['id'].'_'.$img_id]);
				
				$db->db_perform(TABLE_SLIDER_IMAGES, $slider_array, 'update', "image_id = ".$db->db_prepare($img_id)." AND language_id = ".$db->db_prepare((int)$lang['id']));
				$lng_id->MoveNext();
			}
		}
	}
}

function saveSettings() {
	global $db, $message_stack;
	
	$setting = $db->db_query("SELECT
									configuration_key
								FROM
									".TABLE_SLIDER_SETTINGS);

	while(!$setting->EOF) {
		$db->db_query("UPDATE 	
							".TABLE_SLIDER_SETTINGS." 
						SET 
							configuration_value = ".$db->db_prepare($_POST[$setting->fields['configuration_key']])." 
						WHERE 
							configuration_key = ".$db->db_prepare($setting->fields['configuration_key']));
		$setting->MoveNext();
	}
}

function deleteSlider() {
	global $db, $message_stack;
	
	$img = $db->db_query("SELECT
								image_id,
								image_name
							FROM
								".TABLE_SLIDER_IMAGES."
							WHERE
								slider_id = ".$db->db_prepare($_GET['slider_id']));
	if($img->_numOfRows) {
		while(!$img->EOF) { $x++;
			@unlink(DIR_FS_CATALOG_IMAGES.'slider/slider_org/'.$img->fields['image_name']);
			@unlink(DIR_FS_CATALOG_IMAGES.'slider/slider_big/'.$img->fields['image_name']);
			@unlink(DIR_FS_CATALOG_IMAGES.'slider/slider_thumb/'.$img->fields['image_name']);
			$db->db_query("DELETE FROM ".TABLE_SLIDER_IMAGES." WHERE image_id = ".$db->db_prepare($img->fields['image_id']));
			$img->MoveNext();
		}
	}
	$db->db_query("DELETE FROM ".TABLE_SLIDER." WHERE slider_id = ".$db->db_prepare($_GET['slider_id']));
	$message_stack->add_session('Der Slider wurde mit '.$x.' Grafiken gelöscht', 'success');
}

function deletePic() {
	global $db, $message_stack;
	
	$img = $db->db_query("SELECT
								image_name
							FROM
								".TABLE_SLIDER_IMAGES."
							WHERE
								image_id = ".$db->db_prepare($_GET['image_id']));

	@unlink(DIR_FS_CATALOG_IMAGES.'slider/slider_org/'.$img->fields['image_name']);
	@unlink(DIR_FS_CATALOG_IMAGES.'slider/slider_big/'.$img->fields['image_name']);
	@unlink(DIR_FS_CATALOG_IMAGES.'slider/slider_thumb/'.$img->fields['image_name']);
	$db->db_query("DELETE FROM ".TABLE_SLIDER_IMAGES." WHERE image_id = ".$db->db_prepare($_GET['image_id']));

	$message_stack->add_session('Die Grafik wurde gelöscht', 'success');
}